package com.matt.controller.pad;

import com.matt.commons.dto.sso.StaffDTO;
import com.matt.commons.exception.BusinessException;
import com.matt.commons.model.StatusResponse;
import com.matt.domain.Classes;
import com.matt.domain.course.CoursePlanClasses;
import com.matt.foundation.constant.CourseConstant;
import com.matt.foundation.constant.PermissionConstant;
import com.matt.foundation.model.ErrorCode;
import com.matt.foundation.model.MemVerificationCache;
import com.matt.foundation.query.ClassesCoursePlanQuery;
import com.matt.foundation.query.FamilyCoursePlanQuery;
import com.matt.foundation.utils.DateUtil;
import com.matt.foundation.vo.course.PadCoursePlanDetailVo;
import com.matt.foundation.vo.course.PadTeacherCoursePlanListVo;
import com.matt.foundation.vo.nursery.NurseryScheduleDetailVo;
import com.matt.service.ClassesService;
import com.matt.service.CourseEarlyService;
import com.matt.service.CourseNurseryService;
import com.matt.service.NurseryScheduleService;
import com.matt.service.remoteCall.SsoCallService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

import static com.matt.foundation.constant.RequestConstant.TICKET;

/**
 * *******************************
 * Created by Ron.
 * form com.matt
 * on 2018/3/13
 * *******************************
 */
@RestController
@RequestMapping("/pad/course_nursery")
@Api(tags = "移动端 - 排课")
public class PadCourseNurseryController {

    @Autowired
    private CourseNurseryService courseNurseryService;

    @Autowired
    private MemVerificationCache cache;

    @Autowired
    private SsoCallService ssoCallService;

    @Autowired
    private NurseryScheduleService nurseryScheduleService;

    @Autowired
    private CourseEarlyService courseEarlyService;

    @Autowired
    private ClassesService classesService;

    @RequestMapping("/plan_list")
    public StatusResponse planList(@RequestHeader(TICKET) String ticket, ClassesCoursePlanQuery query) {
        StaffDTO baseInfo = cache.getStaff(ticket);
        query.setStaffId(baseInfo.getId());
        query.setAreaId(baseInfo.getAreaId());

        Page<PadTeacherCoursePlanListVo> coursePlanNurseryList = courseNurseryService.getPadCoursePlanList(query, false);
        return StatusResponse.success(coursePlanNurseryList);
    }

    @RequestMapping("/history_list")
    public StatusResponse historyList(@RequestHeader(TICKET) String ticket, ClassesCoursePlanQuery query) {
        StaffDTO baseInfo = cache.getStaff(ticket);
        query.setAreaId(baseInfo.getAreaId());
        query.setStaffId(baseInfo.getId());

        Page<PadTeacherCoursePlanListVo> coursePlanNurseryList = courseNurseryService.getPadCoursePlanHistory(query, false);
        return StatusResponse.success(coursePlanNurseryList);
    }

    @RequestMapping("/plan_detail")
    public StatusResponse planDetail(String planId) {
        PadCoursePlanDetailVo vo = courseNurseryService.getPadCoursePlanDetail(planId);
        return StatusResponse.success(vo);
    }


    @RequestMapping("/begin_course")
    public StatusResponse beginCourse(@RequestHeader(TICKET) String ticket, String coursePlanClassesId, String[] teacherIds) {
        CoursePlanClasses result = courseNurseryService.beginCourse(coursePlanClassesId, teacherIds, cache.getStaffId(ticket), ticket);
        return StatusResponse.success(result);
    }

    @RequestMapping("/today_plan_list")
    public StatusResponse todayPlanList(@RequestHeader(TICKET) String ticket, String classId, ClassesCoursePlanQuery query) {
        Page<PadTeacherCoursePlanListVo> coursePlanNurseryList = null ;
        StaffDTO baseInfo = cache.getStaff(ticket);
        query.setStaffId(baseInfo.getId());
        query.setAreaId(baseInfo.getAreaId());
        query.setClassId(classId);
        String queryType = query.getType();
        Classes classes = classesService.selectClassById(classId);
        if(classes == null){
            throw new BusinessException(ErrorCode.CLASSES_NOT_EXIST, "没有找到班级");
        }

        //查询托班今日课程列表
        if(classes.getCategory() == CourseConstant.Category.NURSERY.codeOf()) {
            coursePlanNurseryList = courseNurseryService.getPadTodayCoursePlanList(query, false,queryType,false);
        }
        if(classes.getCategory() == CourseConstant.Category.EARLY.codeOf()) {
            if ("today".equals(queryType)) {
                //查询早教班今日课程列表
                coursePlanNurseryList = courseEarlyService.getPadTodayCoursePlan(query, false);
            } else if ("week".equals(queryType)) {
                //查询早教班本周课程列表
                coursePlanNurseryList = courseEarlyService.getPadWeekCoursePlan(query, false);
            }else if ("nextweek".equals(queryType)) {
                //查询早教班下周课程列表
                coursePlanNurseryList = courseEarlyService.getPadNextWeekCoursePlan(query, false);
            }
        }

        return StatusResponse.success(coursePlanNurseryList);

    }
    //今日 所有
    @ApiOperation(value = "家庭端-排课记录")
    @PostMapping("/today_plan_family_list")
    public StatusResponse todayPlanFamilyList(@RequestHeader(TICKET) String ticket,  FamilyCoursePlanQuery query) {
        Page<PadTeacherCoursePlanListVo>  coursePlanNurseryList = courseNurseryService.getPadFamilyTodayCoursePlanList(query,query.getType(),false);
        return StatusResponse.success(coursePlanNurseryList);
    }

    @RequestMapping("/week_plan_list")
    public StatusResponse weekPlanList(@RequestHeader(TICKET) String ticket, String classId, ClassesCoursePlanQuery query) {
        List<NurseryScheduleDetailVo> lsit = new ArrayList<>();
        String [] getWeekDay= DateUtil.getWeekDay();
        for(String days :getWeekDay ){
            NurseryScheduleDetailVo nurseryScheduleDetailVo = nurseryScheduleService.scheduleTeacherDetail(classId, days);
            lsit.add(nurseryScheduleDetailVo);
        }
        return StatusResponse.success(lsit);
    }

    @RequestMapping(value ="/today_KX_plan_list",method = RequestMethod.POST)
    public StatusResponse todayKXPlanList(@RequestHeader(TICKET) String ticket, String classId, ClassesCoursePlanQuery query) {
        /*StaffDTO baseInfo = ssoCallService.getStaff(ticket);
        query.setStaffId(baseInfo.getId());
        query.setAreaId(baseInfo.getAreaId());
        query.setClassId(classId);
        Boolean isTestUser = ssoCallService.getIsTestUser(baseInfo.getId());
        Page<PadTeacherCoursePlanListVo> coursePlanNurseryList = courseNurseryService.getPadTodayCoursePlanList(query, isTestUser);
        return StatusResponse.success(coursePlanNurseryList);*/
        String today = DateUtil.millisToDateTime(System.currentTimeMillis(), "yyyy-MM-dd");
        return StatusResponse.success(nurseryScheduleService.scheduleTeacherDetail(classId, today));

    }

    @RequestMapping("/week_KX_plan_list")
    public StatusResponse weekKXPlanList(@RequestHeader(TICKET) String ticket, String classId, ClassesCoursePlanQuery query) {
        List<NurseryScheduleDetailVo> lsit = new ArrayList<>();
        String [] getWeekDay= DateUtil.getWeekDay();
        for(String days :getWeekDay ){
            NurseryScheduleDetailVo nurseryScheduleDetailVo = nurseryScheduleService.scheduleTeacherDetail(classId, days);
            lsit.add(nurseryScheduleDetailVo);
        }
        return StatusResponse.success(lsit);
    }


}
